const fs = require("fs");
const archiver = require("archiver");
const zip = archiver("zip");
class ZipDist {
	constructor(options) {
		this.options = options || {};
		zip.on("error", err => {
			console.log('压缩失败', err);
			throw err;
		});
	}
	apply(compiler) {
		const { outputDir, name } = this.options
		let filepath = `dist${name ? ('/' + name) : ''}.zip`
		compiler.hooks.done.tap("zipDist", () => {
			const output = fs.createWriteStream(filepath);
			zip.pipe(output);
			zip.glob("**", { cwd: outputDir });
			zip.finalize().then(() => {
				console.log('zip file path:' + filepath);
			})
		})
	}
}

module.exports = ZipDist
